#!/usr/bin/env python3
import os


def copy_verbose_and_redirect_output_to_file(src, dest, filepath):
    """复制文件并且打印复制过程, 并且将复制过程重定向到指定文件"""
    cmd = f"cp -v {src} {dest} >> {filepath}"
    os.system(cmd)


def move_verbose_and_redirect_output_to_file(src, dest, filepath):
    """移动文件并且打印移动过程, 并且将移动过程重定向到指定文件"""
    cmd = f"mv -v {src} {dest} >> {filepath}"
    os.system(cmd)


def mkdir_verbose_and_redirect_output_to_file(dirpath, filepath):
    """创建目录并且打印创建过程, 并且将创建过程重定向到指定文件"""
    cmd = f"mkdir -v {dirpath} >> {filepath}"
    os.system(cmd)


def remove_verbose_and_redirect_output_to_file(file, filepath):
    """删除文件并且打印删除过程, 并且将删除过程重定向到指定文件"""
    cmd = f"rm -v {file} >> {filepath}"
    os.system(cmd)


def remove_dir_verbose_and_redirect_output_to_file(dirpath, filepath):
    """删除目录并且打印删除过程, 并且将删除过程重定向到指定文件"""
    cmd = f"rm -rv {dirpath} >> {filepath}"
    os.system(cmd)


import subprocess


# 使用subprocess.run()重定向输出到文件
def copy_verbose_and_redirect_output_to_file_with_subp(src, dest, filepath):
    """复制文件并且打印复制过程, 并且将复制过程重定向到指定文件"""
    cmd = f"cp -v {src} {dest}"
    with open(filepath, "a") as f:
        subprocess.run(cmd, shell=True, stdout=f, stderr=f)


# 普通的 subprocess.run(cmd, stdout=f) 只能把有限的信息输出到文件
# 我需要额外的时间信息以丰富日志, 所以，原本的方法需要改进
# 比对如下：
# 原来的：removed directory 'ttt'
# 改进后的：2021-04-12 21:30:00,000 - INFO - removed directory 'ttt'
# 我或许需要使用 logging 模块！
import subprocess
import logging


def remove_dir_verbose_and_redirect_output_to_file_with_time(dirpath, logfile):
    """借助logging模块, 以及 rm --verbose 选项， 生成带有时间戳的信息，保存此信息到 logfile 对应的文件中"""
    cmd = f"rm -rv {dirpath}"
    with open(logfile, "a") as f:
        subprocess.run(cmd, shell=True, stdout=f, stderr=f)
        # 以下是改进后的代码
        logging.basicConfig(
            filename=logfile,
            level=logging.INFO,
            format="%(asctime)s - %(message)s",
        )
        logging.info(f"removed directory '{dirpath}'")


import time


def cur_time():
    # 返回类似于'2023-04-21 19:06:33,997'的字符串
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


def another_cur_time():
    # 另一种方式实现上面的功能
    return (
        time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        + f",{time.time() % 1 * 1000:.0f}"
    )


# 使用 subprocess 模块运行命令，将命令的输出保存在字符串outpot中
# 使用 logging.info() 将 output 保存到日志文件中
def remove_file_and_output_to_log(file, logfile):
    cmd = f"rm -v {file}"
    res = subprocess.run(cmd, shell=True, capture_output=True)
    logging.basicConfig(
        filename=logfile,
        level=logging.INFO,
        format="%(asctime)s - %(message)s",
    )
    logging.info(res.stdout.decode("utf-8").strip())


if __name__ == '__main__0':
    REPO_PATH = os.path.dirname(os.path.abspath(__file__))
    print(f"REPO_PATH: {REPO_PATH}")

if __name__ == '__main__1':
    copy_verbose_and_redirect_output_to_file("test.py", "test2.py", "test.log")

if __name__ == '__main__2':
    move_verbose_and_redirect_output_to_file("test2.py", "test3.py", "test.log")

if __name__ == '__main__3':
    mkdir_verbose_and_redirect_output_to_file("test_dir", "test.log")

if __name__ == '__main__4':
    remove_verbose_and_redirect_output_to_file("test3.py", "test.log")

if __name__ == '__main__5':
    remove_dir_verbose_and_redirect_output_to_file("test_dir", "test.log")

if __name__ == '__main__6':
    copy_verbose_and_redirect_output_to_file_with_subp(
        "test.py", "test2.py", "test.log"
    )

if __name__ == '__main__7':
    remove_dir_verbose_and_redirect_output_to_file_with_time("ttt", "test.log")

if __name__ == '__main__':
    remove_file_and_output_to_log("test2.py", "test.log")
